package com.online.music.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.online.music.common.Result;
import com.online.music.entity.dto.LikePageDTO;
import com.online.music.entity.po.Playlist;
import com.online.music.utils.UserContextUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import com.online.music.service.ILikeService;
import com.online.music.entity.po.Like;

import org.springframework.web.bind.annotation.RestController;


/**
 * @Author: Leno
 * @Date: 2025-01-31
 * @Description: like接口
 */
@RequiredArgsConstructor
@Slf4j
@Tag(name = "like接口")
@RestController
@RequestMapping("/like")
public class LikeController {

    private final ILikeService likeService;

    @Operation(summary = "查询所有Like")
    @GetMapping
    public Result<List<Like>> findAll() {
        return Result.success(likeService.list());
    }

    @Operation(summary = "根据id查询Like")
    @GetMapping("/{id}")
    public Result<Like> findOne(@PathVariable Long id) {
        return Result.success(likeService.getById(id));
    }

    @Operation(summary = "根据分页查询Like")
    @GetMapping("/page")
    public Result<Page<Playlist>> findPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize,
                                           LikePageDTO like) {
        return likeService.findPage(pageNum, pageSize, like);
    }

    @Operation(summary = "添加或修改Like")
    @PostMapping
    public Result<Boolean> like(@RequestBody Like like) {
        return likeService.like(like);
    }

    @Operation(summary = "取消Like")
    @DeleteMapping("/cancel/like")
    public Result<Boolean> delete(@RequestBody List<Long> playlistIds) {
        return Result.success(
                likeService.lambdaUpdate()
                        .in(Like::getPlaylistId, playlistIds)
                        .eq(Like::getUserId, UserContextUtil.getUserId())
                        .remove()
        );
    }

    @Operation(summary = "根据id批量删除Like")
    @DeleteMapping("/del/batch")
    public Result<String> deleteBatch(@RequestBody List<Long> ids) {
        return likeService.deleteBatch(ids);
    }


}
